<?php	
include("../mysql_connect.php");
include("../error/upload.php");
// Get XML
/*	
	$xmlCreate = '
<ShareGroup UserName="rafale@thetigerparty.com" GroupID="" GroupName="Family" Method="Create">
</ShareGroup>';
	$xmlModify = '
<ShareGroup UserName="rafale@thetigerparty.com" GroupID="32" GroupName="Family" Method="Modify" newName="MyFamily">
	<User ID="4" Name="MemberA" Email="a@family.com"/>
	<User ID="5" Name="MemberB" Email="b@family.com"/>
	<User ID="6" Name="MemberCC" Email="c@family.com"/>
	<User ID="7" Name="MemberD" Email="d@family.com"/>
	<User ID="" Name="MemberE" Email="e@family.com"/>
	<User ID="" Name="MemberF" Email="f@family.com"/>
</ShareGroup>';
	$xmlDelete = '
<ShareGroup UserName="rafale@thetigerparty.com" GroupID="32" GroupName="Family" Method="Delete"/>';

	$xml = simplexml_load_string($xmlDelete);
/*/
	$xml = simplexml_load_file('php://input');
//*/
	// Get ShareGroupAttr
	$GroupAttr = array();
	foreach( $xml->attributes( ) as $attr)
	{
		$GroupAttr[] = (string)$attr;
	}
	
	// Ger Group ID
	$GroupID = $GroupAttr[0];
	// Ger Book ID
	$BookID = $GroupAttr[1];
	// Get User Name
	$UserName = $GroupAttr[2];	
	// Get Status
	$Status = $GroupAttr[3];
	
	
	/**$strSqlCommand = "SELECT idUSER FROM user WHERE Email=\"$GroupAttr[2]\" ";
	if (!$result = mysql_query($strSqlCommand))
 	{
	 	echo "<Error Message=\"".mysql_error()."\" Domain=\"CreateGroup\" />";
	 	echo "<Error Message=\"".$strSqlCommand."\" Domain=\"CreateGroup\" />";
		saveError("GetUserGroup", NULL, date("Y-m-d H:i:s"), mysql_error());
		return;
 	}
	$row = @mysql_fetch_array($result);
		$IDs[] = $row['idUSER'];
	**/
	
	$strSqlCommand = "SELECT MEDIA_idMEDIA FROM spot_user_sb_m 
	                    WHERE STORYBOARD_idSTORYBOARD = ".$BookID." GROUP BY MEDIA_idMEDIA";
	
	$result = mysql_query($strSqlCommand);
	while($rowMedia = @mysql_fetch_array($result))	
	{
		$Media_id = $rowMedia['MEDIA_idMEDIA'];
	
		if ($Status=='Add')
			$strSqlCommand = "INSERT INTO spot_group_media_sb ( GROUP_idGROUP, MEDIA_idMEDIA,  STORYBOARD_idSTORYBOARD) 
								VALUES (".$GroupID.", ".$Media_id.", ".$BookID.")";
		else if ($Status=='Delete')
			$strSqlCommand = "DELETE FROM spot_group_media_sb WHERE GROUP_idGROUP='".$GroupID."' 
								AND MEDIA_idMEDIA='".$Media_id."' AND STORYBOARD_idSTORYBOARD='".$BookID."'";

		if (!mysql_query($strSqlCommand))
 		{
		 	echo "<Error Message=\"".mysql_error()."\" Domain=\"MyBookShareGroup_".$Status."\" />";
		 	echo "<Error Message=\"".$strSqlCommand."\" Domain=\"MyBookShareGroup_".$Status."\" />";
			saveError("MyBook_ShareGroup", NULL, date("Y-m-d H:i:s"), mysql_error());
			exit;
 		}
	}
	
	echo "<Success Message=\"The requested Information was accepted\" Domain=\"MyBookShareGroup_".$Status."\"/>";
?>